
/*******************************************************************************
File: rent_index_acs
Purpose: estimate state-level Phillips curves for rents using ACS data.
Last Update: Oct 1st, 2021.
*******************************************************************************/




clear all
set more off
 set matsize 800
eststo clear
clear matrix

 
* Set the local name with your name.
local name = "juan"


* set your path under your name
if "`name'" == "juan" { 
cd "/Users/juanherreno/Dropbox/Investigacion/StateLevelCPIs/ReplicationPackage/code_to_share"
}

if  "`name'" == "emi" {
cd "c:/mydocs/dropbox/StateLevelCPIs/ReplicationPackage/code_to_share"
}

if  "`name'" == "joe" {
cd "/Users/joehazell/Dropbox/StateLevelCPIs/ReplicationPackage/code_to_share"
}

if  "`name'" == "jon" {
cd ""
}


if "`name'" == "juan_pc" { 
cd "C:/Users/jdh2181/Dropbox/Investigacion/StateLevelCPIs/ReplicationPackage/code_to_share"
}


use "acs_rent_data.dta", clear

drop  plumbing // Not too much variation in this variable.

* Keep rented units.

keep if rent != 0
drop if rent < 100

* Hedonic regressions. By year, run a regression of rent on characteristics.
gen logrent = log(rent)
gen eta = . // error term in the regression
gen gamma = . // time fixed effect



* Compute gamma and eta by running a regression by year.
levelsof year, local(yearlist)
foreach kk of local yearlist{
	reghdfe logrent if year == `kk' [w = hhwt], residuals absorb(acrehous kitchen rooms unitsstr bedrooms builtyr2)
	predict eta_temp, resid
	replace eta = eta_temp if year == `kk'
	matrix beta_`kk' = e(b)
	svmat double beta_`kk'
	carryforward beta_`kk', replace
	replace gamma =  beta_`kk' if year == `kk'
	drop eta_temp beta*
}
gen temporal = exp(gamma + eta) // Residual price for each property
gen agg_index = exp(gamma) // Collect the time fixed effect


* Aggregate the residual prices at the state-level
by year statefip, sort: egen sum_wt = sum(hhwt)
gen adjw = hhwt/sum_wt
gen wt_temporal = temporal*adjw
by year statefip, sort: egen mean_rent = sum(wt_temporal)


* Compute simple averages of rents
gen rent_wt = rent*adjw
by year statefip, sort: egen rent_state_simple = sum(rent_wt)
by year, sort: egen sum_wt_nat = sum(hhwt)
gen adjw_nat = hhwt/sum_wt_nat
gen rent_wt_nat = rent*adjw_nat
by year, sort: egen rent_state_simple_nat = sum(rent_wt_nat)


fcollapse (mean) mean_rent agg_index rent_state_simple rent_state_simple_nat, by(year statefip)
xtset statefip year


gen state_rent_inflation = (mean_rent - l.mean_rent)/l.mean_rent
gen nat_rent_inflation = (agg_index - l.agg_index)/l.agg_index
gen state_rent_inflation_simple = (rent_state_simple - l.rent_state_simple)/l.rent_state_simple
gen nat_rent_inflation_simple = (rent_state_simple_nat - l.rent_state_simple_nat)/l.rent_state_simple_nat

save "working/rent_inflation.dta", replace

********************************************************************************

use "working/data_for_rents_reg.dta", clear

destring fips, gen(statefip)

fcollapse mean_une  L4_mean_une u_sum_20, by(year statefip)
merge 1:1 statefip year using "working/rent_inflation.dta"
replace state_rent_inflation = 100*state_rent_inflation
*gen infl_overall = mean_infl_services0*(1-0.3) + state_rent_inflation*0.3
gen infl_reg_rent = -state_rent_inflation

generate infl_reg_rent_time_agg = infl_reg_rent / 4


tsset statefip year
eststo clear
clear matrix
gen constant = 1
eststo: ivreghdfe infl_reg_rent L4_mean_une, absorb(constant) cluster(statefip)
eststo: ivreghdfe infl_reg_rent L4_mean_une, absorb(statefip ) cluster(statefip)
eststo: ivreghdfe infl_reg_rent L4_mean_une, absorb(statefip year) cluster(statefip)
esttab using "Tables/psi_rent.tex", prehead("") posthead("") prefoot("") postfoot("") varlabels(L4_mean_une "$\psi$") plain  cells(b(star fmt(%9.3f)) se(par)) stats(, fmt(%7.0f %7.1f %7.2f)) nolabel replace mlabels(none) collabels(none) stardrop(L4_mean_une)


eststo clear
eststo: ts2sls infl_reg_rent_time_agg (u_sum_20 = L4_mean_une), absorb(constant) cluster(statefip)
eststo: ts2sls infl_reg_rent_time_agg (u_sum_20 = L4_mean_une), absorb(statefip) cluster(statefip)
eststo: ts2sls infl_reg_rent_time_agg (u_sum_20 = L4_mean_une) i.year, absorb(statefip) cluster(statefip)
esttab, se keep(u_sum_20)

disp("Number of observations is ")
count if !missing(infl_reg_rent_time_agg) & !missing(L4_mean_une) & !missing(u_sum_20) 
estout using "Tables/kappa_rent.tex", style(tex) keep(u_sum_20) varlabels(u_sum_20 "$\kappa$") cells(b(star fmt(%9.4f)) se(par)) stats( , fmt(%7.0f %7.1f %7.2f)) nolabel replace mlabels(none) collabels(none) stardrop(u_sum_20)

